package se480.tracker.util;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.Logger;

import se480.tracker.TrackerConstants;

/**
 * Writes out the query responses received by the QueryClients
 * 
 * @author mmichalak
 *
 */
public class OutputFileWriterUtil {
	static Logger logger = Logger.getLogger(OutputFileWriterUtil.class.getName());
	
	static BufferedWriter OUT = null;
	
	static {
		try {
			OutputFileWriterUtil.OUT = new BufferedWriter(new FileWriter("c:\\SE480\\" + TrackerConstants.OUTPUT_FILENAME));
		} catch (IOException e) {
			logger.error("Unable to create TrackerOutput file!");
			e.printStackTrace();
		}
	}
	
	public static void writeToFile(String message) {
        try {
        	if (message == null) {
        		logger.error("Output message is null");
        		return;
        	}
        	OUT.write(message);
        	OUT.write("\n\n");
        	OUT.flush();
		} catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
    }
	    
    public static void close() {
        try {
        	OUT.close();
		} catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}

}
